<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllermetadata //extends AppController
{

	function __construct(  )
	{
		
		$task	=	application::getTask();//( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			
				
			case 'getItem':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters 			=	application::getParameters();
								
					$result 	=	$this->getItem(
													application::getUser_id(),
													$parameters->id);
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);

	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }

				
				break;
				
			case 'save':
				
				
				try {
        						
								
					$parameters		=	application::getParameters();
								
					$id				=	$parameters->id;
					
					if(empty($id)){
						//sem id, então não é para criar um novo
						
						$result=array("status"=>0,
									"data"=>MSG_26_METADATA_ID_NAO_INFORMADO,
									"notification"=>26);
			
					}else{
						
						$this->save($parameters);
						
						//$result=array("status"=>"e-mail Unknown");
						//$result=array("status"=>"update");
						$result=array("status"=>1,
									"data"=>MSG_27_METADATA_SAVE_SUCESSO,
									"notification"=>27);
					}
					
					
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
		
				
				
				break;
					
			
			
			case 'out':
				
				
				try {
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(false);
					
								
					$parameters =	application::getParameters();
								
					if( $parameters->metadata ){
						
						$result 	=	$this->getViewMetaDataFromKey(
														$parameters->key,
														$parameters->notation);
														
					}else{
						
						$result 	=	$this->getViewDataFromKey(
														$parameters->key,
														$parameters->notation);
														
					}			
								
			
					application::executeView( 'notation', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			  //  catch (Exception $e) {
			   ////     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			  //  }
				
		
				
				
				break;	
			default:
				
				//echo "view não definida.";
				throw new AppException( MSG_23_METODO_NAO_DEFINIDO );
				
				break;
		}
		
	}
	

	
	

	
	public function getItem( $user_id, $id ){
				
		$result=false;
		
		try{
			
			$model	=	application::getModel('metadata');

			$result	=	$model->getItem( $user_id, $id );

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		//$query = sprintf("SELECT  * FROM %s WHERE user_id=%d;",
        //         	configuration::table_view, application::getUser_id());

		

		return $result;
		
	}
	
	
	
	
	
	
	
	public function save( $parameters ){
				
		$result=false;
				
		try {
        
			$model	=	application::getModel('metadata');
	
			$result	=	$model->save( $parameters );

	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
}